//
//  HomeHDTypeVC.m
//  jingdongfang
//
//  Created by LJL on 2018/5/14.
//  Copyright © 2018年 BOE. All rights reserved.
//

#import "HomeHDTypeVC.h"
#import "HomeHDCell.h"
#import "IGHomeHDTypeApi.h"
#import "IGPictureListDetailVC.h"

@interface HomeHDTypeVC ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>{
    NSString *pageNumber;
    NSString *pageSize;
    NSString *queryTime;//查询时间
}
@property (nonatomic, strong) UICollectionView *collectionV;
@property (nonatomic, strong) IGHomeHDTypeApi *api;
@property (nonatomic, strong) NSMutableArray *hdArr;
@end

@implementation HomeHDTypeVC

-(UICollectionView *)collectionV
{
    if (!_collectionV)
    {
        _collectionV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, KSCREENHEIGHT) collectionViewLayout:[[UICollectionViewFlowLayout alloc] init]];
        _collectionV.dataSource = self;
        _collectionV.delegate = self;
        _collectionV.backgroundColor = [UIColor c3Color];
        [_collectionV registerClass:[HomeHDCell class] forCellWithReuseIdentifier:@"HomeHDCell"];
        __weak typeof(self) weakSelf = self;
        [_collectionV ig_setHeaderWithRefreshingBlock:^{
            pageNumber = @"0";
            [weakSelf dataRequest];
        }];
    }
    return _collectionV;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.view addSubview:self.collectionV];
    [self initBaseData];
    [self dataRequest];
}

- (void)initBaseData
{
    self.api = [[IGHomeHDTypeApi alloc] init];
    pageNumber = @"0";
    pageSize = @"10";
    queryTime = @"";
    self.hdArr = [@[] mutableCopy];
//    self.type = @"1";
}

- (void)dataRequest
{
    if ([self.type isEqualToString:@""])
    {
        self.type = @"1";
    }
    @KWeakObj(self)
    self.api.type = _type;
    self.api.pageNumber = pageNumber;
    self.api.pageSize = pageSize;
    self.api.queryTime = queryTime;
    [self.api startWithDecodeClass:[IGHomeHDTypeResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        [_collectionV ig_endHeaderRefreshing];
        [_collectionV ig_endFooterRefreshing];
        if (responseObject)
        {
            IGHomeHDTypeResp *resp = responseObject;
            IGHomeHDTypeModel *model = resp.data;
            if (model.lstDrawlist && model.lstDrawlist.count > 0)
            {
                [selfWeak setupFooter];
            }
            else
            {
                [_collectionV ig_endFooterRefreshingWithNoMoreData];
            }
            queryTime = model.time;
            
            if ([pageNumber isEqualToString:@"0"])
            {
                _hdArr = [NSMutableArray arrayWithArray:model.lstDrawlist];
            }
            else
            {
                [_hdArr addObjectsFromArray:model.lstDrawlist];
            }
            [_collectionV reloadData];
        }
        else
        {
            //空白页处理逻辑写在这里
        }
    }];
}

- (void)setupFooter
{
    @KWeakObj(self)
    [_collectionV ig_setFooterWithRefreshingBlock:^{
        pageNumber = [NSString stringWithFormat:@"%ld", [pageNumber integerValue] + 1];
        [selfWeak dataRequest];
    }];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _hdArr.count ? : 0;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake((KSCREENWIDTH - 3) / 2, (KSCREENWIDTH - 3) / 2 + 38);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HomeHDCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeHDCell" forIndexPath:indexPath];
    cell.isBig = YES;
    IGDrawModel *hdModel = _hdArr[indexPath.row];
    cell.hdModel = hdModel;
    return cell;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionVeliew layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 3;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    
    return 0;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
        if (self.hdArr.count > 0)
        {
            IGDrawModel *hd = self.hdArr[indexPath.row];
            IGPictureListDetailVC *vc = [[IGPictureListDetailVC alloc] init];
            vc.drawlistId = hd.drawId;
            [UIViewController ig_pushViewController:vc animated:YES];
        }
}

@end

